In [1]:
# Import libraries and modules
import numpy as np
import Synthesis as synt
import time
import Utils as ut
import TestCase as tc
from copy import deepcopy
from qiskit_textbook.tools import array_to_latex
In [2]:
# Testcase preparation
U = ut.generateRandomU(nq=4, nc=20) # Generate random matrix
#U = tc.u # or Use already manually generated matrises in TestCase.py
In [3]:
# Show input matrix
ut.matrix_to_latex(U) 
Out[3]:
$\displaystyle \left[\begin{array}{cccccccccccccccc}- \frac{\sqrt{2} \omega^{3}}{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & - \frac{\sqrt{2} \omega^{3}}{2} & 0\\0 & 0 & 0 & 0 & 0 & \omega^{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\frac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{\sqrt{2} \omega}{2} & - \frac{1}{2} & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \omega & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\frac{\omega^{3}}{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{\sqrt{2}}{2} & - \frac{\omega^{3}}{2} & 0\\0 & 0 & 0 & 0 & 0 & 0 & \omega^{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\end{array}\right]$
In [4]:
# Check if the matrix is unitary (if so return True)
ut.checkUnitarity(U)
Out[4]:
True
In [5]:
# Pass the matrix to synthesis function and call it, then save the return values
# The main return value is Clifford+T Circuit saved in cliffordTCirc variable
# The sceond return value is a higher level circuit made up of only controlled gates, saved in mcgCirc variable
# The third return value is high level components including only 2 level matrises only, saved in HLCs2 variable
# The last return value is high level components including both 2 and 1 level matrises, saved in HLCs12 variable
start_time = time.time() 
cliffordTCirc, mcgCirc, HLCs2, HLCs12 = synt.syntCliffordTCircuit(deepcopy(U))
exe_time = time.time() - start_time # measure execution time
In [6]:
# Show high level componets, 1 and 2 level matrises
HLCs12
Out[6]:
$\displaystyle T_{[7,14]}^{7} H_{[7,14]} T_{[1,14]}^{5} H_{[1,14]} X_{[1,14]} \omega_{[1]}^{7} X_{[2,4]} X_{[3,6]} X_{[4,6]} \omega_{[6]}^{2} X_{[7,15]} \omega_{[7]}^{2} \omega_{[10]} X_{[12,13]} X_{[14,15]} \omega_{[14]} \omega_{[15]}^{7}$
In [7]:
# Show high level matrises, only 2 level matrises(1 levels converted into 2 levels)
HLCs2
Out[7]:
$\displaystyle T_{[7,14]}^{7} H_{[7,14]} T_{[1,14]}^{5} H_{[1,14]} X_{[1,14]} X_{[1,2]} T_{[1,2]}^{7} X_{[1,2]} X_{[2,4]} X_{[3,6]} X_{[4,6]} T_{[1,6]}^{2} X_{[7,15]} T_{[1,7]}^{2} T_{[1,10]} X_{[12,13]} X_{[14,15]} T_{[1,14]} T_{[1,15]}^{7}$
In [8]:
# Show the circuit(made of only controlled gates)
mcgCirc.draw()
Out[8]:
In [9]:
# Evaluate mcg circuit
result = ut.assess(U, mcgCirc)[0]

# Show the result (True if the circuit is correct)
result
Out[9]:
True
In [10]:
# show the circuit(made of only Clifford+T gates)
cliffordTCirc.draw()
Out[10]:
In [11]:
# Evaluate Clifford+T circuit
# Uin is the input matrix (in numerical form)
# Uout is the actuall matrix of generated circuit (in numerical form)
result1,Uin,Uout = ut.assess(U, mcgCirc)

# Show the result (True if the circuit is correct)
result1
Out[11]:
True
In [12]:
# Show input matrix
array_to_latex(Uin)
$\displaystyle \begin{bmatrix} \tfrac{1}{2}(1 - i) & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \tfrac{1}{2}(1 - i) & 0 \\ 0 & 0 & 0 & 0 & 0 & i & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \tfrac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \tfrac{1}{2}(1 + i) & -\tfrac{1}{2} & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 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 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \tfrac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \tfrac{1}{2}(1 + i) & -\tfrac{1}{2} & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \tfrac{1}{\sqrt{2}}(1 + i) & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ \tfrac{1}{\sqrt{8}}(-1 + i) & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \tfrac{1}{\sqrt{2}} & \tfrac{1}{\sqrt{8}}(1 - i) & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & i & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \end{bmatrix} $$ $
In [14]:
# Compare 
# Uin == Uout
In [15]:
# number of clifford+T gates
len(cliffordTCirc.data)
Out[15]:
5722
In [16]:
# synthesis execuation time(in seconds)
print(exe_time,'s')
10.194831848144531 s
In [ ]: